﻿// listbox
(function() {

$(window).data('gui').plugins['listbox'] =
{

  makeHTML : function(param)
  {
    var obj = document.createElement("div");
	param.parent.appendChild(obj);
	obj.setAttribute('data-effect','box');
	var background = document.createElement("div");
	background.setAttribute('data-effect','header');
	obj.appendChild(background);
    var div = document.createElement("div");
	obj.appendChild(div);
	div.setAttribute('data-effect','body');
    
    var table = document.createElement("table");
	div.appendChild(table);

	//debugger;
	var thead = document.createElement('thead');
	table.appendChild(thead);
	var hxtr = $(param.xml).find('thead>tr');	
	if (hxtr.length>0)
	{
		
		
		for(var i=0;i < hxtr.length; i++)
		{
			var tr = document.createElement('tr');
			thead.appendChild(tr);
			
			var tr2 = document.createElement('tr');
			thead.appendChild(tr2);
		
			var xth = $(hxtr.get(i)).children('th');
			for(var j=0;j < xth.length; j++)
			{
				var th = document.createElement('th');
				tr.appendChild(th);
				var div = document.createElement('div');
				th.appendChild(div);
				
				var th2 = document.createElement('th');
				tr2.appendChild(th2);
				var div2 = document.createElement('div');
				th2.appendChild(div2);
			
				var html = $(xth.get(j)).text();
				$(div).html(html);
				$(div2).html(html);
			}
			//thtag.setAttribute('style','min-width:' + size[i] + ';');
		}
	}
	/*
	$(obj).off('onload');
					
	$(obj).on("onload", function(event) {
		console.info(this);
		var s = $gui(this).columnsSize();
		$gui(this).columnsSize(s);
	});
	*/
	
	var bxtr = $(param.xml).find('tbody');
	if (bxtr.length == 1)
	{
		var tbody = document.createElement('tbody');
		table.appendChild(tbody);
		guiTareNameSpace.executeChildrensPlugin(bxtr[0],tbody);
	}

		
	var datasize = param.xml.getAttribute('data-size');
	if (datasize !== null) {
		var sizes = datasize.split(' ');
		
		for (var s = 0;s < sizes.length; s++)
		{
			if (sizes[s] !== undefined && sizes[s] !== null)
			{
				
				if (table.tHead !== undefined)
				{
					$(table.tHead.rows[0].cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
					$(table.tHead.rows[0].cells[s]).css('width' , sizes[s]+'px', 'important');
					
					$(table.tHead.rows[1].cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
					$(table.tHead.rows[1].cells[s]).css('width' , sizes[s]+'px','important');
				}
				
				if (table.tBodies.length>0)
				{	
					for (var b = 0; b < table.tBodies[0].rows.length; b++)
					{
						$(table.tBodies[0].rows.item(b).cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
						$(table.tBodies[0].rows.item(b).cells[s]).css('width' , sizes[s]+'px' ,'important');
					}
				}
			}
		}
	}
	
	
    return  obj;
  },
 
  makeXML : function(xml)
  {
		var element = xml.ownerDocument.createElement("listbox");

		var table = this.jquery.find('table');
		if (table.length == 0) return null;
	
	/*
	
    var elem = table[0].tHead.rows[0]; //.item(0);
    if (!elem || elem === undefined) return null;

		var size = '';
		var header = '';

		for (var c = 0;c < elem.cells.length; c++)
		{
			var css = $(elem.cells[c]).css('min-width');
			var label = $(elem.cells[c]).html().trim();

			if (!css) css = '70px';
			size += css + '\t';

			if (!label) label = 'unknow';
			header += label + '\t';
		}

		var tagsize = xml.ownerDocument.createElement("size");
		var taghead = xml.ownerDocument.createElement("head");

		var tabsize = xml.ownerDocument.createTextNode(size.trim());
		var tabhead = xml.ownerDocument.createTextNode(header.trim());

		tagsize.appendChild(tabsize);
		taghead.appendChild(tabhead);

		element.appendChild(tagsize);
		element.appendChild(taghead);

		var body = xml.ownerDocument.createElement("body");

		var nbrow = this.getLength();

		for (var r = 0; r < nbrow ; r++)
		{
			var ligne = this.row(r).join('\t').trim();
			var tagrow = xml.ownerDocument.createElement("row");
			var tabrow = xml.ownerDocument.createTextNode(ligne);
			tagrow.appendChild(tabrow);

			var value = this.getValue(r);
			if (value) tagrow.setAttribute('value',value);

			body.appendChild(tagrow);
		}

		
		
		element.appendChild(body);
	*/
		return element;

  },

  deleteAllRows : function()
  {
    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return this;
    }

    while( table[0].tBodies[0].rows.length )
    {
      table[0].tBodies[0].deleteRow( 0 );
    }

    return this;
  },

  deleteRow : function(indice)
  {
    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return [];
    }
    table[0].tBodies[0].deleteRow( indice );
    return this;
  },

  insertRow : function(indice,value,row)
  {

    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return [];
    }

	var sizes = this.columnsSize();
	
    var r = table[0].tBodies[0].insertRow(indice);
    $(r).attr('data-value',value);
	
	r.setAttribute('data-type','tr');
	r.setAttribute('data-effect', 'row');
    for (var cell =0; cell < row.length; cell++)
    {
		  var cl = r.insertCell(cell);
		  cl.setAttribute('data-type','td');
		  //$(cl).text(row[cell]);
		  var uixml = null;
		  //debugger;
		try
		{
		   uixml = $.parseXML('<null>' + row[cell] + '</null>');
		} catch(e)
		{
			uixml = null;
		}
		finally
		{
		  	if (uixml === null) $(cl).text(row[cell])
			else
			{	  
				guiTareNameSpace.executeChildrensPlugin(uixml.documentElement,cl);
			}
		}
			
//		if (indice == 0)
		{
			$(cl).css('maxWidth' , sizes[cell]+'px', 'important');
			$(cl).css('width' , sizes[cell]+'px' ,'important');						
		}
    }

    return this;
  },

  columnsSize : function(sizes)
  {
		if (sizes === undefined)
		{
			var ret = [];
			var datasize = this[0].getAttribute('data-size');
			if (datasize !== null) ret = datasize.split(' ');
			return ret;
		} else if (sizes instanceof Array)
		{
			var table = this.jquery.find('table');
			if (table.length == 0)
			{
				console.error('-> Table element not found');
				return this;
			}
			
			this[0].getAttribute('data-size',sizes.join(' ').trim());
			for (var s = 0;s < sizes.length; s++)
			{
				if (sizes[s] !== undefined && sizes[s] !== null)
				{
					if (table.tHead !== undefined)
					{
						$(table.tHead.rows[0].cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
						$(table.tHead.rows[0].cells[s]).css('width' , sizes[s]+'px', 'important');
						
						$(table.tHead.rows[1].cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
						$(table.tHead.rows[1].cells[s]).css('width' , sizes[s]+'px','important');
					}
					
					if (table.tBodies.length>0)
					{	
						for (var b = 0; b < table.tBodies[0].rows.length; b++)
						{
							$(table.tBodies[0].rows.item(b).cells[s]).css('maxWidth' , sizes[s]+'px', 'important');
							$(table.tBodies[0].rows.item(b).cells[s]).css('width' , sizes[s]+'px' ,'important');
						}
					}
				}
			}
		}
		return this;
  },
  
  
  row : function(indice)
  {
		var ret= [];
    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return ret;
    }

    var elem = table[0].tBodies[0].rows.item(indice);
    if (!elem || elem === undefined)
    {
      console.error('-> indice is out range');
      return ret;
    }

		for (var c = 0;c < elem.cells.length; c++)
		{
			ret.push($(elem.cells[c]).html().trim());
		}

    return ret;
  },

  getValue :function(indice)
  {
    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return null;
    }

    var elem = table[0].tBodies[0].rows.item(indice);
    if (!elem || elem === undefined)
    {
      console.error('-> indice is out range');
      return null;
    }

    return elem.getAttribute('data-value');
  },

  getValueSelectedRaw: function(event)
  {
			var target = event.target || event.srcElement;
      var tr = $(target).parents('tr');
	if (tr.length == 0) return null;
      return tr[0].getAttribute('data-value');
  },

  header: function(values)
  {
		var table = this.jquery.find('table');
		if (table.length == 0)
		{
			console.error('-> Table element not found');
			return this;
		}
		
		if (values === undefined)
		{
			var ret = [];
			for (var c = 0;c < table[0].tHead.rows[0].cells.length; c++)
			{
				ret.push($(table[0].tHead.rows[0].cells[c].children[0]).html().trim());
			}
			return ret;
		} else if (values instanceof Array)
		{
			for (var c = 0;c < table[0].tHead.rows[0].cells.length; c++)
			{
				if (values[c] !== undefined && values[c] !== null)
				{
					$(table[0].tHead.rows[0].cells[c].children[0]).html(values[c]);
					$(table[0].tHead.rows[1].cells[c].children[0]).html(values[c]);
				}
			}
		}
		return this;
  },

  
  getSelectedRaw: function(event)
  {
		var ret= [];
		var target = event.target || event.srcElement;
    var tr = $(target).parents('tr');
	if (tr.length == 0) return ret;

	for (var c = 0;c < tr[0].cells.length; c++)
	{
		ret.push($(tr[0].cells[c]).html().trim());
	}

    	return ret;
  },

  getLength : function()
  {
    var table = this.jquery.find('table');
    if (table.length == 0)
    {
      console.error('-> Table element not found');
      return this;
    }

    return table[0].tBodies[0].rows.length;
  }
  }
})();
